<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="委外编号" prop="externalCode">
        <el-input
          v-model="queryParams.externalCode"
          placeholder="请输入委外编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="证件号" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入证件号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="共案证件号" prop="jointCaseIdNumber">
        <el-input
          v-model="queryParams.jointCaseIdNumber"
          placeholder="请输入共案证件号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="caseType">
        <el-radio-group v-model="queryParams.caseType">
          <el-radio :label="1">在催案件</el-radio>
          <el-radio :label="0">全部案件</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['information:case:query']" type="primary" icon="el-icon-search" size="mini"
                   @click="handleQuery">搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['information:case:import']"
        >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['information:case:reorder']" type="primary" icon="el-icon-d-caret" size="mini">调序
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="caseList">
      <el-table-column width="150" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            @click="openInfo(scope.row.id)"
            v-hasPermi="['information:case:info']">查看详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column width="150" label="分公司" align="center" prop="branchOffice"/>
      <el-table-column width="150" label="委外编号" align="center" prop="externalCode"/>
      <el-table-column width="150" label="身份证号" align="center" prop="idNumber"/>
      <el-table-column width="150" label="客户" align="center" prop="customerName"/>
      <el-table-column width="150" label="手次" align="center" prop="customerOrder"/>
      <el-table-column label="共案案件开始时间" align="center" prop="jointCaseStartDate" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jointCaseStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="共案案件结束时间" align="center" prop="jointCaseEndDate" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jointCaseEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column width="150" label="共案身份证号" align="center" prop="jointCaseIdNumber"/>
      <el-table-column width="150" label="共案案件数量" align="center" prop="jointCaseNumber"/>
      <el-table-column width="150" label="共案委案金额之和" align="center" prop="jointCaseAmount"/>
      <el-table-column label="共案回收金额之和" align="center" prop="recoveryAmount" width="200"/>
      <el-table-column label="最近还款时间" align="center" prop="latestRepaymentDate" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.latestRepaymentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最近还款金额" align="center" prop="latestRepaymentAmount" width="200"/>
      <el-table-column width="150" label="数据状态" align="center" prop="dataStateDescribe"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入20MB以内的xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <CaseInfo ref="caseInfo"/>
  </div>
</template>

<script>
import { listCase } from '@/api/information/case'
import { getToken } from '@/utils/auth'
import CaseInfo from './info'

export default {
  name: 'Case',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 共同的案件总表格数据
      caseList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        externalCode: null,
        idNumber: null,
        jointCaseIdNumber: null,
        caseType: 1
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/information/case/importExcel'
      }
    }
  },
  components: { CaseInfo },
  created() {
    this.getList()
  },
  methods: {
    /** 详情*/
    openInfo(id) {
      this.$refs['caseInfo'].open(id)
    },
    /** 查询共同的案件总列表 */
    getList() {
      this.loading = true
      listCase(this.queryParams).then(response => {
        this.caseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '信息导入'
      this.upload.open = true
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('information/case/importTemplate', {}, `information_template_${new Date().getTime()}.xlsx`)
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert('<div style=\'overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;\'>' + response.msg + '</div>', '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    }
  }
}
</script>
